{% set pypi_name = 'watcher' %}
{% set source = fetch_source("https://tarballs.openstack.org/watcher/watcher-master.tar.gz") %}
{% set upstream_version = upstream_version() %}
{% set rpm_release = '1' %}

%global with_doc 1
%if 0%{?rhel} || 0%{?fedora}
%global rdo 1
%endif
%if 0%{?rhel} || 0%{?fedora}
%global wsgi_dir %{_datarootdir}/{{ pypi_name }}
%else
%global wsgi_dir /srv/www/{{ pypi_name }}
%endif
Name:           {{ py2name() }}
Version:        {{ py2rpmversion() }}
Release:        {{ py2rpmrelease() }}
Epoch:          {{ epoch('{{ pypi_name }}') }}
Summary:        Resource optimization service for OpenStack. (Wathcer)
License:        {{ license('Apache-2.0') }}
Group:          Development/Languages/Python
URL:            https://docs.openstack.org/{{ pypi_name }}
Source0:        {{ source|basename }}
Source1:        openstack-watcher-applier.service
Source2:        openstack-watcher-decision-engine.service
Source3:        openstack-watcher-api.service
BuildRequires:  openstack-macros
BuildRequires:  {{ py3('APScheduler') }}
BuildRequires:  {{ py3('PrettyTable') }}
BuildRequires:  {{ py3('SQLAlchemy') }}
BuildRequires:  {{ py3('WSME') }}
BuildRequires:  {{ py3('WebOb') }}
BuildRequires:  {{ py3('cinderclient') }}
BuildRequires:  {{ py3('coverage') }}
BuildRequires:  {{ py3('croniter') }}
BuildRequires:  {{ py3('freezegun') }}
BuildRequires:  {{ py3('glanceclient') }}
BuildRequires:  {{ py3('gnocchiclient') }}
BuildRequires:  {{ py3('ironicclient') }}
BuildRequires:  {{ py3('jsonpatch') }}
BuildRequires:  {{ py3('jsonschema') }}
BuildRequires:  {{ py3('keystoneauth1') }}
BuildRequires:  {{ py3('keystoneclient') }}
BuildRequires:  {{ py3('keystonemiddleware') }}
BuildRequires:  {{ py3('lxml') }}
BuildRequires:  {{ py3('microversion_parse') }}
BuildRequires:  {{ py3('mock') }}
BuildRequires:  {{ py3('monascaclient') }}
BuildRequires:  {{ py3('networkx') }}
BuildRequires:  {{ py3('neutronclient') }}
BuildRequires:  {{ py3('novaclient') }}
BuildRequires:  {{ py3('openstackclient') }}
BuildRequires:  {{ py3('os-api-ref') }}
BuildRequires:  {{ py3('oslo.cache') }}
BuildRequires:  {{ py3('oslo.concurrency') }}
BuildRequires:  {{ py3('oslo.context') }}
BuildRequires:  {{ py3('oslo.db') }}
BuildRequires:  {{ py3('oslo.log') }}
BuildRequires:  {{ py3('oslo.messaging') }}
BuildRequires:  {{ py3('oslo.policy') }}
BuildRequires:  {{ py3('oslo.reports') }}
BuildRequires:  {{ py3('oslo.serialization') }}
BuildRequires:  {{ py3('oslo.service') }}
BuildRequires:  {{ py3('oslo.upgradecheck') }}
BuildRequires:  {{ py3('oslo.utils') }}
BuildRequires:  {{ py3('oslo.versionedobjects') }}
BuildRequires:  {{ py3('oslotest') }}
BuildRequires:  {{ py3('pbr') }}
BuildRequires:  {{ py3('pecan') }}
BuildRequires:  {{ py3('six') }}
BuildRequires:  {{ py3('stestr') }}
BuildRequires:  {{ py3('stevedore') }}
BuildRequires:  {{ py3('taskflow') }}
Requires:       python3-{{ pypi_name }} = %{epoch}:%{version}-%{release}
BuildArch:      noarch
%if 0%{?suse_version}
BuildRequires:  apache2-mod_wsgi
BuildRequires:  systemd-rpm-macros
Requires:       apache2-mod_wsgi
%{?systemd_requires}
%else
BuildRequires:  python3-mod_wsgi
BuildRequires:  systemd
Requires:       python3-mod_wsgi
Requires(post): systemd
Requires(postun): systemd
Requires(preun): systemd
%endif

%description
OpenStack Watcher provides a flexible and scalable resource optimization
service for multi-tenant OpenStack-based clouds.
Watcher provides a robust framework to realize a wide range of cloud
optimization goals, including the reduction of data center
operating costs, increased system performance via intelligent virtual machine
migration, increased energy efficiency and more!

%package -n python3-{{ pypi_name }}
Summary:        Resource optimization service for OpenStack. (Wathcer) - Python Module
Group:          Applications/System
Requires:       {{ py3('APScheduler') }}
Requires:       {{ py3('PasteDeploy') }}
Requires:       {{ py3('PrettyTable') }}
Requires:       {{ py3('SQLAlchemy') }}
Requires:       {{ py3('WSME') }}
Requires:       {{ py3('WebOb') }}
Requires:       {{ py3('python-cinderclient') }}
Requires:       {{ py3('croniter') }}
Requires:       {{ py3('enum34') }}
Requires:       {{ py3('python-glanceclient') }}
Requires:       {{ py3('gnocchiclient') }}
Requires:       {{ py3('python-ironicclient') }}
Requires:       {{ py3('jsonpatch') }}
Requires:       {{ py3('jsonschema') }}
Requires:       {{ py3('keystoneauth1') }}
Requires:       {{ py3('python-keystoneclient') }}
Requires:       {{ py3('keystonemiddleware') }}
Requires:       {{ py3('lxml') }}
Requires:       {{ py3('microversion_parse') }}
Requires:       {{ py3('python-monascaclient') }}
Requires:       {{ py3('networkx') }}
Requires:       {{ py3('python-neutronclient') }}
Requires:       {{ py3('python-novaclient') }}
Requires:       {{ py3('openstackclient') }}
Requires:       {{ py3('oslo.cache') }}
Requires:       {{ py3('oslo.db') }}
Requires:       {{ py3('oslo.messaging') }}
Requires:       {{ py3('oslo.policy') }}
Requires:       {{ py3('oslo.reports') }}
Requires:       {{ py3('oslo.serialization') }}
Requires:       {{ py3('oslo.service') }}
Requires:       {{ py3('oslo.upgradecheck') }}
Requires:       {{ py3('oslo.versionedobjects') }}
Requires:       {{ py3('pbr') }}
Requires:       {{ py3('pecan') }}
Requires:       {{ py3('six') }}
Requires:       {{ py3('stevedore') }}
Requires:       {{ py3('taskflow') }}
Recommends:     {{ py3('python-ceilometerclient') }}
BuildArch:      noarch

%description -n python3-{{ pypi_name }}
OpenStack Watcher provides a flexible and scalable resource optimization
service for multi-tenant OpenStack-based clouds.
Watcher provides a robust framework to realize a wide range of cloud
optimization goals, including the reduction of data center
operating costs, increased system performance via intelligent virtual machine
migration, increased energy efficiency and more!

This package contains the Watcher Python library.

%package decision-engine
Summary:        OpenStack Watcher - Decision-Engine
Group:          System/Management
Requires:       %{name} = %{epoch}:%{version}-%{release}

%description decision-engine
This package contains the OpenStack Watcher decision-engine.

%package applier
Summary:        OpenStack Watcher - Applier
Group:          System/Management
Requires:       %{name} = %{epoch}:%{version}-%{release}

%description applier
This package contains the OpenStack Watcher Applier.

%package api
Summary:        OpenStack Watcher - Api
Group:          System/Management
Requires:       %{name} = %{epoch}:%{version}-%{release}

%description api
This package contains the OpenStack Watcher Api.

%if 0%{?with_doc}
%package -n {{ py2name() }}-doc
Summary:        Documentation for OpenStack Watcher Service
Group:          Documentation/HTML
BuildRequires:  fdupes
BuildRequires:  {{ py3('Sphinx') }}
BuildRequires:  {{ py3('keystoneclient') }}
BuildRequires:  {{ py3('os-api-ref') }}
BuildRequires:  {{ py3('sphinxcontrib-apidoc') }}
BuildRequires:  {{ py3('sphinxcontrib-pecanwsme') }}
BuildRequires:  {{ py3('sphinxcontrib-seqdiag') }}
BuildRequires:  {{ py3('sphinxcontrib-svg2pdfconverter') }}
BuildArch:      noarch

%description -n {{ py2name() }}-doc
OpenStack Watcher documentation.

This package contains the documentation
%endif


%prep
%autosetup -n python-{{ pypi_name }}-{{ upstream_version }}
%py_req_cleanup

%build
%{py3_build}

%if 0%{?with_doc}
PYTHONPATH=.  PBR_VERSION={{ upstream_version }} %sphinx_build -b html doc/source doc/build/html
rm -rf doc/build/html/.{doctrees,buildinfo}
%fdupes doc/build/html/
%endif

# policy/config file generation
PYTHONPATH=. oslo-config-generator --config-file=etc/watcher/oslo-config-generator/watcher.conf \
    --output-file etc/watcher/watcher.conf.sample
PYTHONPATH=. oslopolicy-sample-generator --config-file=etc/watcher/oslo-policy-generator/watcher-policy-generator.conf  \
    --output-file etc/watcher/policy.yaml.sample

sed -e "
    s|%WATCHER_SERVICE_PORT%|9322|g;
    s|%WATCHER_WSGI_DIR%|%{wsgi_dir}|g;
    s|%USER%|%{apache_user}|g;
    s|%APIWORKERS%|2|g;
    s|%APACHE_NAME%|apache2|g;
" -i devstack/files/apache-watcher-api.template

%install
%{py3_install}
%python_expand %fdupes %{buildroot}%{$python_sitelib}

# Install service configuration files
install -d -m 755 %{buildroot}%{_sysconfdir}/{{ pypi_name }}
install -d -m 755 %{buildroot}%{_sysconfdir}/{{ pypi_name }}/{{ pypi_name }}.conf.d

# (re)move install artifacts
mv %{buildroot}%{_prefix}%{_sysconfdir}/{{ pypi_name }}/* %{buildroot}%{_sysconfdir}/{{ pypi_name }}
rm -r %{buildroot}%{_prefix}%{_sysconfdir}/apache2
rm -r %{buildroot}%{_sysconfdir}/{{ pypi_name }}/oslo-{config,policy}-generator/
rm %{buildroot}%{_sysconfdir}/{{ pypi_name }}/README-{{ pypi_name}}.conf.txt

# Install apache configuration files
install -d -m 755 %{buildroot}%{apache_conf_dir}/
install -p -D -m 644 devstack/files/apache-watcher-api.template %{buildroot}%{apache_conf_dir}/{{ pypi_name }}.conf.sample

# Install wsgi file
install -d -m 755 %{buildroot}%{wsgi_dir}
install -p -D -m 644 watcher/api/app.wsgi %{buildroot}%{wsgi_dir}

# Install service file
install -p -D -m 755 %{SOURCE1} %{buildroot}%{_unitdir}/openstack-watcher-applier.service
install -p -D -m 755 %{SOURCE2} %{buildroot}%{_unitdir}/openstack-watcher-decision-engine.service
install -p -D -m 755 %{SOURCE3} %{buildroot}%{_unitdir}/openstack-watcher-api.service

%pre
%openstack_pre_user_group_create watcher watcher /sbin/nologin

%post decision-engine
%systemd_post openstack-watcher-decision-engine.service

%preun decision-engine
%systemd_preun openstack-watcher-decision-engine.service

%postun decision-engine
%systemd_postun_with_restart openstack-watcher-decision-engine.service

%post applier
%systemd_post openstack-watcher-applier.service

%preun applier
%systemd_preun openstack-watcher-applier.service

%postun applier
%systemd_postun_with_restart openstack-watcher-applier.service

%post api
%systemd_post openstack-watcher-api.service

%preun api
%systemd_preun openstack-watcher-api.service

%postun api
%systemd_postun_with_restart openstack-watcher-api.service

%files
%{_bindir}/{{ pypi_name }}-api-wsgi
%{_bindir}/{{ pypi_name }}-db-manage
%{_bindir}/{{ pypi_name }}-sync
%{_bindir}/{{ pypi_name }}-status
%dir %attr(0770, root, %{apache_group}) %{wsgi_dir}
%attr(0770, root, %{apache_group}) %{wsgi_dir}/app.wsgi
%attr(0770, root, %{apache_group}) %{apache_conf_dir}/{{ pypi_name }}.conf.sample
%dir %config(noreplace) %attr(0750, root, watcher) %{_sysconfdir}/{{ pypi_name }}
%config(noreplace) %attr(0640, root, watcher) %{_sysconfdir}/{{ pypi_name }}/policy.yaml.sample
%config(noreplace) %attr(0640, root, watcher) %{_sysconfdir}/{{ pypi_name }}/{{ pypi_name }}.conf.sample
%dir %attr(0750, root, watcher) %{_sysconfdir}/{{ pypi_name }}/{{ pypi_name }}.conf.d/

%files -n python3-{{ pypi_name}}
%license LICENSE
%doc AUTHORS README.rst
%{python3_sitelib}/{{ pypi_name }}/
%{python3_sitelib}/python_{{ pypi_name }}-*.egg-info

%files decision-engine
%defattr(-,root,root,-)
%{_bindir}/{{ pypi_name }}-decision-engine
%{_unitdir}/openstack-watcher-decision-engine.service

%files applier
%defattr(-,root,root,-)
%{_bindir}/{{ pypi_name }}-applier
%{_unitdir}/openstack-watcher-applier.service

%files api
%defattr(-,root,root,-)
%{_bindir}/{{ pypi_name }}-api
%{_unitdir}/openstack-watcher-api.service

%if 0%{?with_doc}
%files -n {{ py2name() }}-doc
%license LICENSE
%doc doc/build/html
%endif

%changelog
